package StudyCode.summary.myfile;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class AddFileSeparator {

	public static void main(String[] args) {
		System.out.println("加入文件分隔符号: " + pathJoin("/", "E", "Copy", "1", "test.txt"));
	}

	public static String pathJoin(String separator, String... parts) {
		StringBuilder builder = new StringBuilder();
		if (parts.length == 0)
			return "";
		if (parts.length == 1)
			return parts[0];
		for (int i = 0; i < parts.length; i++) {
			String part = parts[i];
			if (i == 0) {
				builder.append(removeEnd(part, separator))
						.append(separator);
			} else if (i == parts.length - 1) {
				builder.append(removeStart(part, separator));
			} else {
				builder.append(
						removeStart(removeEnd(part, separator), separator))
						.append(separator);
			}
		}
		return builder.toString();
	}

	private static String removeEnd(String str, String remove) {
		if (str.endsWith(remove)) {
			return str.substring(0, str.length() - remove.length());
		} else {
			return str;
		}
	}

	private static String removeStart(String str, String remove) {
		if (str.startsWith(remove)) {
			return str.substring(remove.length());
		} else {
			return str;
		}
	}
}
